home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / malloc.h < prev    next >
C/C++ Source or Header  |  1994-05-03  |  9KB  |  282 lines

  1. /* Declarations for `malloc' and friends.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.           Written May 1989 by Mike Haertel.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  22.  
  23. #ifndef _MALLOC_H
  24.  
  25. #define _MALLOC_H    1
  26.  
  27. #ifdef _MALLOC_INTERNAL
  28.  
  29. #ifdef    HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32.  
  33. #if    defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  34. #include <string.h>
  35. #else
  36. #ifndef memset
  37. #define    memset(s, zero, n)    bzero ((s), (n))
  38. #endif
  39. #ifndef memcpy
  40. #define    memcpy(d, s, n)        bcopy ((s), (d), (n))
  41. #endif
  42. #endif
  43.  
  44. #if    defined(__GNU_LIBRARY__) || defined(__STDC__)
  45. #include <limits.h>
  46. #else
  47. #define    CHAR_BIT    8
  48. #endif
  49.  
  50. #ifdef    HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53.  
  54. #endif    /* _MALLOC_INTERNAL.  */
  55.  
  56.  
  57. #ifdef    __cplusplus
  58. extern "C"
  59. {
  60. #endif
  61.  
  62. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
  63. #undef    __P
  64. #define    __P(args)    args
  65. #undef    __ptr_t
  66. #define    __ptr_t        void *
  67. #else /* Not C++ or ANSI C.  */
  68. #undef    __P
  69. #define    __P(args)    ()
  70. #undef    const
  71. #define    const
  72. #undef    __ptr_t
  73. #define    __ptr_t        char *
  74. #endif /* C++ or ANSI C.  */
  75.  
  76. #ifdef    __STDC__
  77. #include <stddef.h>
  78. #else
  79. #undef    size_t
  80. #define    size_t        unsigned int
  81. #undef    ptrdiff_t
  82. #define    ptrdiff_t    int
  83. #endif
  84.  
  85. #ifndef    NULL
  86. #define    NULL    0
  87. #endif
  88.  
  89.  
  90. /* Allocate SIZE bytes of memory.  */
  91. extern __ptr_t malloc __P ((size_t __size));
  92. /* Re-allocate the previously allocated block
  93.    in __ptr_t, making the new block SIZE bytes long.  */
  94. extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
  95. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  96. extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
  97. /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  98. extern void free __P ((__ptr_t __ptr));
  99.  
  100. /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  101. extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
  102.  
  103. /* Allocate SIZE bytes on a page boundary.  */
  104. extern __ptr_t valloc __P ((size_t __size));
  105.  
  106.  
  107. #ifdef _MALLOC_INTERNAL
  108.  
  109. /* The allocator divides the heap into blocks of fixed size; large
  110.    requests receive one or more whole blocks, and small requests
  111.    receive a fragment of a block.  Fragment sizes are powers of two,
  112.    and all fragments of a block are the same size.  When all the
  113.    fragments in a block have been freed, the block itself is freed.  */
  114. #define INT_BIT        (CHAR_BIT * sizeof(int))
  115. #define BLOCKLOG    (INT_BIT > 16 ? 12 : 9)
  116. #define BLOCKSIZE    (1 << BLOCKLOG)
  117. #define BLOCKIFY(SIZE)    (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  118.  
  119. /* Determine the amount of memory spanned by the initial heap table
  120.    (not an absolute limit).  */
  121. #define HEAP        (INT_BIT > 16 ? 4194304 : 65536)
  122.  
  123. /* Number of contiguous free blocks allowed to build up at the end of
  124.    memory before they will be returned to the system.  */
  125. #define FINAL_FREE_BLOCKS    8
  126.  
  127. /* Data structure giving per-block information.  */
  128. typedef union
  129.   {
  130.     /* Heap information for a busy block.  */
  131.     struct
  132.       {
  133.     /* Zero for a large block, or positive giving the
  134.        logarithm to the base two of the fragment size.  */
  135.     int type;
  136.     union
  137.       {
  138.         struct
  139.           {
  140.         size_t nfree;    /* Free fragments in a fragmented block.  */
  141.         size_t first;    /* First free fragment of the block.  */
  142.           } frag;
  143.         /* Size (in blocks) of a large cluster.  */
  144.         size_t size;
  145.       } info;
  146.       } busy;
  147.     /* Heap information for a free block
  148.        (that may be the first of a free cluster).  */
  149.     struct
  150.       {
  151.     size_t size;        /* Size (in blocks) of a free cluster.  */
  152.     size_t next;        /* Index of next free cluster.  */
  153.     size_t prev;        /* Index of previous free cluster.  */
  154.       } free;
  155.   } malloc_info;
  156.  
  157. /* Pointer to first block of the heap.  */
  158. extern char *_heapbase;
  159.  
  160. /* Table indexed by block number giving per-block information.  */
  161. extern malloc_info *_heapinfo;
  162.  
  163. /* Address to block number and vice versa.  */
  164. #define BLOCK(A)    (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  165. #define ADDRESS(B)    ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  166.  
  167. /* Current search index for the heap table.  */
  168. extern size_t _heapindex;
  169.  
  170. /* Limit of valid info table indices.  */
  171. extern size_t _heaplimit;
  172.  
  173. /* Doubly linked lists of free fragments.  */
  174. struct list
  175.   {
  176.     struct list *next;
  177.     struct list *prev;
  178.   };
  179.  
  180. /* Free list headers for each fragment size.  */
  181. extern struct list _fraghead[];
  182.  
  183. /* List of blocks allocated with `memalign' (or `valloc').  */
  184. struct alignlist
  185.   {
  186.     struct alignlist *next;
  187.     __ptr_t aligned;        /* The address that memaligned returned.  */
  188.     __ptr_t exact;        /* The address that malloc returned.  */
  189.   };
  190. extern struct alignlist *_aligned_blocks;
  191.  
  192. /* Instrumentation.  */
  193. extern size_t _chunks_used;
  194. extern size_t _bytes_used;
  195. extern size_t _chunks_free;
  196. extern size_t _bytes_free;
  197.  
  198. /* Internal version of `free' used in `morecore' (malloc.c). */
  199. extern void _free_internal __P ((__ptr_t __ptr));
  200.  
  201. #endif /* _MALLOC_INTERNAL.  */
  202.  
  203. /* Underlying allocation function; successive calls should
  204.    return contiguous pieces of memory.  */
  205. extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
  206.  
  207. /* Default value of `__morecore'.  */
  208. extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
  209.  
  210. /* If not NULL, this function is called after each time
  211.    `__morecore' is called to increase the data size.  */
  212. extern void (*__after_morecore_hook) __P ((void));
  213.  
  214. /* Nonzero if `malloc' has been called and done its initialization.  */
  215. extern int __malloc_initialized;
  216.  
  217. /* Hooks for debugging versions.  */
  218. extern void (*__free_hook) __P ((__ptr_t __ptr));
  219. extern __ptr_t (*__malloc_hook) __P ((size_t __size));
  220. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  221.  
  222. /* Return values for `mprobe': these are the kinds of inconsistencies that
  223.    `mcheck' enables detection of.  */
  224. enum mcheck_status
  225.   {
  226.     MCHECK_DISABLED = -1,    /* Consistency checking is not turned on.  */
  227.     MCHECK_OK,            /* Block is fine.  */
  228.     MCHECK_FREE,        /* Block freed twice.  */
  229.     MCHECK_HEAD,        /* Memory before the block was clobbered.  */
  230.     MCHECK_TAIL            /* Memory after the block was clobbered.  */
  231.   };
  232.  
  233. /* Activate a standard collection of debugging hooks.  This must be called
  234.    before `malloc' is ever called.  ABORTFUNC is called with an error code
  235.    (see enum above) when an inconsistency is detected.  If ABORTFUNC is
  236.    null, the standard function prints on stderr and then calls `abort'.  */
  237. extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
  238.  
  239. /* Check for aberrations in a particular malloc'd block.  You must have
  240.    called `mcheck' already.  These are the same checks that `mcheck' does
  241.    when you free or reallocate a block.  */
  242. extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
  243.  
  244. /* Activate a standard collection of tracing hooks.  */
  245. extern void mtrace __P ((void));
  246.  
  247. /* Statistics available to the user.  */
  248. struct mstats
  249.   {
  250.     size_t bytes_total;        /* Total size of the heap. */
  251.     size_t chunks_used;        /* Chunks allocated by the user. */
  252.     size_t bytes_used;        /* Byte total of user-allocated chunks. */
  253.     size_t chunks_free;        /* Chunks in the free list. */
  254.     size_t bytes_free;        /* Byte total of chunks in the free list. */
  255.   };
  256.  
  257. /* Pick up the current statistics. */
  258. extern struct mstats mstats __P ((void));
  259.  
  260. /* Call WARNFUN with a warning message when memory usage is high.  */
  261. extern void memory_warnings __P ((__ptr_t __start,
  262.                   void (*__warnfun) __P ((const char *))));
  263.  
  264.  
  265. /* Relocating allocator.  */
  266.  
  267. /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
  268. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
  269.  
  270. /* Free the storage allocated in HANDLEPTR.  */
  271. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  272.  
  273. /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
  274. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, size_t __size));
  275.  
  276.  
  277. #ifdef    __cplusplus
  278. }
  279. #endif
  280.  
  281. #endif /* malloc.h  */
  282.